﻿@page "/deferredcreds"
@inject IState<DeferredCredentialsState> deferredCredentialsState
@inject IDispatcher dispatcher
@inject NotificationService notificationService
@inject NavigationManager navigationManager
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@using SimpleIdServer.CredentialIssuer.Domains;
@using SimpleIdServer.CredentialIssuer.Website.Stores.CredentialIssuer;
@using System.Globalization;

<div class="row">
    <div class="col">
        <RadzenText class="mt-3 mb-3 no-margin" Text="@Global.DeferredCredentialsTitle" TextStyle="TextStyle.DisplayH3" />
    </div>
</div>

<RadzenDataGrid @ref="deferredCredentialsGrid"
                AllowFiltering="true"
                AllowColumnResize="true"
                AllowAlternatingRows="false"
                AllowSorting="true"
                PageSize="30"
                AllowPaging="true"
                PagerHorizontalAlign="HorizontalAlign.Left"
                ShowPagingSummary="true"
                IsLoading="@deferredCredentialsState.Value.IsLoading"
                Count="@deferredCredentialsState.Value.DeferredCredentials.Count"
                Data="@deferredCredentialsState.Value.DeferredCredentials.Content"
                LoadData="@LoadData"
                TItem="Domains.DeferredCredential"
                ColumnWidth="300px">
    <Columns>
        <RadzenDataGridColumn TItem="Domains.DeferredCredential" Filterable="false" Sortable="false" Width="80px" TextAlign="TextAlign.Center" Title="@Global.Name">
            <Template Context="data">
                <RadzenLink Text="@data.Configuration.Type" Path="@($"/deferredcreds/{data.TransactionId}")" />
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="Domains.DeferredCredential" Property="FormatterName" Filterable="false" Sortable="false" Title="@Global.Format" Width="80px" />
        <RadzenDataGridColumn TItem="Domains.DeferredCredential" Filterable="false" Sortable="false" Title="@Global.Status" Width="80px">
            <Template Context="data">
                <RadzenBadge Text="@Enum.GetName(typeof(DeferredCredentialStatus), data.Status)" IsPill="true" />
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="Domains.DeferredCredential" Property="CreateDateTime" Filterable="false" Sortable="true" FormatString="{0:dd/M/yyyy HH:mm:ss}" SortOrder="SortOrder.Descending" Title="@Global.CreateDateTime" Width="80px" />
    </Columns>
</RadzenDataGrid>

@code {
    RadzenDataGrid<Domains.DeferredCredential> deferredCredentialsGrid;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            if (!deferredCredentialsState.Value.IsLoading)
                deferredCredentialsGrid.Reload();
        }
    }

    void LoadData(LoadDataArgs args)
    {
        var act = new SearchDeferredCredentialsAction { Filter = args.Filter, OrderBy = args.OrderBy, Skip = args.Skip, Take = args.Top };
        dispatcher.Dispatch(act);
    }
}